<?php
namespace common\models\goods;

use Yii;
use yii\db\ActiveRecord;
use yii\behaviors\TimestampBehavior;
use common\enums\StatusEnum;
use common\helpers\RegularHelper;

/**
 * This is the model class for table "{{%goods_common}}".
 *
 */
class GoodsCommon extends ActiveRecord
{
    /**
     * {@inheritdoc}
     */
    public static function tableName()
    {
        return '{{%goods_common}}';
    }

    /**
     * {@inheritdoc}
     */
    public function rules()
    {
        return [
            [['goods_name'], 'required'],
            [['gc_id','member_id','store_id','brand_id','goods_lock','goods_verify','goods_commend','selt_at','status', 'created_at', 'updated_at'], 'integer'],
            [['goods_price', 'goods_costprice', 'goods_costprice','goods_discount'], 'number'],
            [['goods_name','goods_code','goods_bar','store_name','brand_name','goods_serial'], 'string', 'max' => 50],
            [['goods_unit'], 'string', 'max' => 10],
            [['gc_name', 'spec_name','goods_stateremark','goods_verifyremark'], 'string', 'max' => 200],
            [['goods_image'],'string','max'=>100],
            [['spec_value','goods_attr','goods_body','goods_specname'], 'string'],
        ];
    }

    /**
     * {@inheritdoc}
     */
    public function attributeLabels()
    {
        return [
            'goods_commonid' => 'ID',
            'goods_name' => '商品名称',
            'goods_code' => '商品编码',
            'goods_bar' => '商品条形码',
            'goods_unit' => '单位',
            'gc_id' => '商品分类',
            'gc_name' => '商品分类',
            'member_id' => '用户id',
            'store_id' => '店铺id',
            'store_name' => '店铺名称',
            'spec_name' => '规格名称',
            'spec_value' => '规格值',
            'brand_id' => '品牌id',
            'brand_name' => '品牌名称',
            'goods_image' => '商品主图',
            'goods_attr' => '商品属性',
            'goods_body' => '商品内容',
            'status' => '商品状态',//商品状态 0下架，1正常，10违规（禁售）
            'goods_stateremark' => '违规原因',
            'goods_verify' => '商品审核',//商品审核 1通过，0未通过，10审核中
            'goods_verifyremark' => '审核失败原因',
            'goods_lock' => '商品锁定',//商品锁定 0未锁，1已锁
            'created_at' => '添加时间',
            'updated_at' => '更新时间',
            'selt_at' => '上架时间',
            'goods_specname' => '规格名称序列化',//规格名称序列化（下标为规格id）
            'goods_price' => '商品价格',
            'goods_marketprice' => '市场价',
            'goods_costprice' => '成本价',
            'goods_discount' => '折扣',
            'goods_serial'=>'规格型号',
            'goods_commend' => '商品推荐',//商品推荐 1是，0否，默认为0
            'goods_freight' => '运费',//运费 0为免运费
        ];
    }

    /**
     * 场景
     *
     * @return array
     */
    public function scenarios()
    {
        return [
//            'backendCreate' => ['goods_name', 'goods_code','goods_bar','goods_unit','gc_id','member_id','store_id'],
            'default' => array_keys($this->attributeLabels()),
        ];
    }

    /**
     * 获取分类对象
     * @return \yii\db\ActiveQuery
     */
    public function getGclass(){
        return $this->hasOne(GClass::className(),['id'=>'gc_id'])
            ->select(['id','name']);
    }

    /**
     * 获取品牌对象
     * @return \yii\db\ActiveQuery
     */
    public function getBrand(){
        return $this->hasOne(Brand::className(),['id'=>'brand_id'])
            ->select(['id','name','logo']);
    }

    /**
     * 获取图片列表
     * @return \yii\db\ActiveQuery
     */
    public function getImage_list(){
        return $this->hasMany(GoodsImage::className(),['goods_commonid'=>'goods_commonid'])
            ->select(['goods_image_id','goods_commonid','goods_image','goods_image_sort','is_default','is_show'])
            ->where(['is_show'=>1]);
    }

    /**
     * 获取商品列表
     * @return \yii\db\ActiveQuery
     */
    public function getGoods_list(){
        return $this->hasMany(Goods::className(),['goods_commonid'=>'goods_commonid'])
            ->select(['goods_id','goods_code','goods_bar','goods_price','goods_marketprice','goods_storage','goods_commonid','status'])
            ->with('attr_index')
            ->where(['status'=>1]);
    }

    /**
     * 获取属性
     * @return \yii\db\ActiveQuery
     */
    public function getAttr(){
        return $this->hasMany(Attr::className(),['goods_commonid'=>'goods_commonid'])
            ->select(['attr_id','attr_name','attr_show','attr_sort','goods_commonid'])
            ->with('values');
//        return $this->hasMany(Attr::className(),['attr_id'=>'attr_id'])
//            ->viaTable('rf_goods_attr_index',['goods_commonid'=>'goods_commonid']);
    }

    /**
     * @return array
     */
    public function behaviors()
    {
        return [
            [
                'class' => TimestampBehavior::class,
                'attributes' => [
                    ActiveRecord::EVENT_BEFORE_INSERT => ['created_at', 'updated_at'],
                    ActiveRecord::EVENT_BEFORE_UPDATE => ['updated_at'],
                ],
            ],
        ];
    }
}
